package com.uu.safeguard.controller;

import com.uu.safeguard.result.Result;
import com.uu.safeguard.result.ResultFactory;
import com.uu.safeguard.service.MemberService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/myuser")
public class MyUserController {

    private Logger logger = LoggerFactory.getLogger(MyUserController.class);

    @Resource
    MemberService memberService;

    /**
     * @Description: 用户详情
     * @Param: [token, operatorMemberSn, params]
     **/
    @PostMapping("/projectUserMessage")
    public Result projectUserMessage(@RequestHeader("token")String token,
                                  @RequestParam Map<String, String> params){
        String memberSn = params.get("memberSn");
        //查询用户及所在小区
        LinkedHashMap<String, Object> memberAndProject = memberService.findMemberAndProject(memberSn);
        //查询是否充电
        //余额转让次数
        LinkedHashMap<String, Object> memberMoneyMakeOver = memberService.findMemberMoneyMakeOver(memberSn);
        memberAndProject.putAll(memberMoneyMakeOver);
        //消费次数及消费金额
        memberAndProject.putAll(memberService.findMemberConsumeMsg(memberSn,null,null));
        //充值次数及充值金额
        memberAndProject.putAll(memberService.findMemberRechargeMsg(memberSn,null));

        return ResultFactory.buildSuccessResult(memberAndProject);
    }
    /**
     * @Description: 小区用户明细（模糊key查询）
     * @Param: [token, operatorMemberSn, params]
     **/
    @PostMapping("/projectUserList")
    public Result projectUserList(@RequestHeader("token")String token,
                                 @RequestParam Map<String, String> params){
        String keywords = params.get("keywords");
        String limit = params.get("limit");
        String page = params.get("page");
        List<LinkedHashMap<String, Object>> projectUserList =
                memberService.findProjectUserList(null,null,keywords,Integer.parseInt(page),Integer.parseInt(limit));
        Integer projectUserListCount =
                memberService.findProjectUserListCount(null,null,keywords);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count",projectUserListCount);
        jsonObject.put("list",projectUserList);
        return ResultFactory.buildSuccessResult(jsonObject);
    }
}
